<?php

namespace app\mp\admin;

use app\system\admin\Admin;

/**
 * 标签池
 * @author 飞燕
 *
 */
class MaterialTags extends Admin
{
    protected $hisiModel = 'MaterialTags';//模型名称[通用添加、修改专用]
    protected $hisiValidate = 'MaterialTags';
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize() {
    	parent::initialize();
    }
    /**
     * 标签池
     * @author 飞燕
     * @date 2020年9月24日
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $keyword    = $this->request->param('keyword/s');
            $where      = $data = [];
            if ($keyword) {
                $where[] = ['name', 'like', "%{$keyword}%"];
            }
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 20);
            $order = 'id desc';
            $systemUserKV = getSystemUserKV(null);
            $data['data'] = db('material_tags')->where($where)->page($page)->limit($limit)->order($order)->select();
            $data['count'] = db('material_tags')->where($where)->count('id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
    	}
    	$assign = [];
    	return $this->assign($assign)->fetch();
    }

    //设置是否验布，缩水，采购表显示等
    public function edit(){
        if ($this->request->isPost()) {// 处理数据优化
            $materialNameKeywords=$this->request->param('name/s');
            $need_checkcloth=$this->request->param('need_checkcloth/d',0);
            $need_shrink=$this->request->param('need_shrink/d',0);
            $caigou_show=$this->request->param('caigou_show/d',0);
            $need_checkcloth=$need_checkcloth==2 ? 0 :$need_checkcloth;
            $need_shrink=$need_shrink==2 ? 0 :$need_shrink;
            $orderMaterials=db('order_material om')
                ->join('material m', "om.material_id=m.id", 'left')
                ->where([['is_caigou_start','eq',0],['m.name','like',"%$materialNameKeywords%"]])->field('mid,om.need_checkcloth,om.need_shrink,caigou_show')->select();
            $midArr=array_column($orderMaterials,'mid');
            $ominfo['need_checkcloth']=$need_checkcloth;
            $ominfo['need_shrink']=$need_shrink;
            $ominfo['caigou_show']=$caigou_show;
            db("order_material")->where([['mid','in',$midArr]])->update($ominfo);
        }
        return parent::edit();
    }
}

